package loveATM.atm.Panel;

import loveATM.User;
import loveATM.atm.ATM;
import loveATM.atm.Font.MyFont;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 登录界面
 */
public class LoginPanel extends JPanel implements ActionListener {
    private ATM atm;
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JButton loginBtn = new JButton(new ImageIcon("images/buttonLand.jpg"));
    MyFont myFont = new MyFont();
    private int errorCount = 3;
    public LoginPanel(ATM atm05){
        this.atm = atm05;
        this.setLayout(null);
        accountLabel.setBounds(300,200,80,50);
        accountLabel.setFont(myFont.font);
        accountTextField.setBounds(380,200,250,50);
        accountTextField.setFont(myFont.font);
        pwdLabel.setBounds(300,250,80,50);
        pwdLabel.setFont(myFont.font);
        pwdField.setBounds(380,250,250,50);
        pwdField.setFont(myFont.font);
        loginBtn.setBounds(420,440,140,50);
        loginBtn.addActionListener(this);
        this.add(accountLabel);
        this.add(accountTextField);
        this.add(pwdLabel);
        this.add(pwdField);
        this.add(loginBtn);

        this.setOpaque(false);
    }
    public int getErrorCount() {
        return errorCount;
    }
    public void setErrorCount(int errorCount) {
        this.errorCount = errorCount;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        ArrayList<User> user = atm.getUsers();
        String pwd = new String(pwdField.getPassword());
        for ( int i = 0 ; i < user.size();i++){
            if (accountTextField.getText().equals(user.get(i).getAccount()) && pwd.equals(user.get(i).getPwd())){
                atm.showPanel("menu");
                atm.index = i;
                accountTextField.setText("");
                pwdField.setText("");
                return;
            }
        }
        errorCount--;
        if (errorCount == 0){
            JOptionPane.showMessageDialog(this,"输入有误,卡已被吞");
            System.exit(0);
        }else {
            JOptionPane.showMessageDialog(this,"输入有误,您还有" +  errorCount + "次机会");
            accountTextField.setText("");
            pwdField.setText("");
        }

    }
}
